package com.lanyou.cook.camel.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.cook.camel.entity.Ftp;
import com.lanyou.cook.camel.util.ExceptionToStrFactory;

public class FtpDao extends BaseDao<Ftp> {
	private static Logger logger = Logger.getLogger(FtpDao.class);
	RowMapper<Ftp> mapper = new RowMapper<Ftp>() {
		public Ftp mapRow(ResultSet rs, int rowNum) throws SQLException {
			Ftp rcd = new Ftp();
			rcd.setId(rs.getString("IF_Ftp_ID"));
			rcd.setSystemId(rs.getString("FIELD_SYSTEM_ID"));
			rcd.setSystemCode(rs.getString("SYSTEM_CODE"));
			rcd.setName(rs.getString("IF_NAME"));
			rcd.setCode(rs.getString("IF_CODE"));
			rcd.setIsPublic(rs.getString("IS_PUBLIC"));
			rcd.setType(rs.getString("IF_TYPE"));
			rcd.setDesc(rs.getString("IF_DESC"));
			rcd.setVersion(rs.getString("IF_VERSION"));
			rcd.setStatus(rs.getString("IF_STATUS"));
			rcd.setOverTimes(Float.parseFloat(rs.getString("OVERTIME_TIMES")));
			rcd.setIsCost(rs.getString("IS_COST"));
			rcd.setRemark(rs.getString("REMARK"));
			rcd.setAuditStatus(rs.getString("AUDIT_STATUS"));
			rcd.setAuditRemark(rs.getString("AUDIT_REMARK"));
			rcd.setIsEnable(rs.getString("IS_ENABLE"));
			rcd.setIsSchedule(rs.getString("IS_SCHEDULE"));
			rcd.setFileCachePath(rs.getString("FILE_CACHE_PATH"));
			rcd.setCreator(rs.getString("CREATOR"));
			rcd.setCreatedDate(rs.getDate("CREATED_DATE"));
			rcd.setModifier(rs.getString("MODIFIER"));
			rcd.setLastUpdateDate(rs.getDate("LAST_UPDATED_DATE"));
			return rcd;
		}
	};

	@Override
	public Ftp findByCode(String code) {
		
//		logger.info("[FtpDao.findByCode]:into findByCode code:---" + code);
		if (StringUtils.isNotBlank(code));
		String sql = "SELECT * FROM t_esb_bu_if_ftp WHERE IS_ENABLE='1' and if_code = ?";
		Ftp ftp = new Ftp();
		try {
			ftp = this.getJdbcTemplate().queryForObject(sql, mapper, code);
			return ftp;
		} catch (DataAccessException e) {
			logger.error("[FtpDao.findByCode]:DataAccessException code:--" + code+"---"+ExceptionToStrFactory.getErrorInfoFromException(e));
			return null;
		}
	};

	/**
	 * （待扩展）
	 * 
	 * @param whereFtp
	 *            封装查询条件的Ftp实例
	 * @return 根据where查询出来的ftpList
	 */
	public List<Ftp> findByWhere(Ftp whereFtp) {
		String sql = "SELECT * FROM t_esb_bu_if_ftp WHERE AUDIT_STATUS = ? and IF_STATUS = ? and IS_ENABLE = ? and IS_SCHEDULE=?";
		List<Ftp> ftpList = new ArrayList<Ftp>();
		Object[] objects = { whereFtp.getAuditStatus(), whereFtp.getStatus(),
				whereFtp.getIsEnable(), whereFtp.getIsSchedule() };
		ftpList = this.getJdbcTemplate().query(sql, mapper, objects);
		return ftpList;
	}
}
